![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
Dont Use Deftype StatementsIn the following code, the first statement makes Visual Basic automatically allocate variables starting with the letters A through D as integers if their type is not explicitly given. Then the second line declares the variable data_points. Because the statement does not explicitly give the variable a data type, and because the variables name begins with the letter d, Visual Basic makes the variable an integer. DefInt A-D Dim data_points Visual Basic has other commands such as DefLng and DefBool to define other default data types. These statements made sense in early versions of Basic and Fortran where variables had to have very short names. If names can only have six characters, starting all integer variables with I can make it easier to understand the purpose of a variable. For example, INOCHR might be an integer holding the number of characters used by a routine. If you follow this naming convention, DefInt I makes sense because all variables starting with I are integers by convention. Modern Visual Basics variables are not as restricted as those in ancient Basic and Fortran. Now you can name this variable num_characters to make its meaning clear. Because variables can have more descriptive names, the initial I is not needed (but see the following section on Hungarian notation). Even if you use Deftype statements, an explicit declaration overrides the default data type. If every integer variable does not start with I and not every variable starting with I is an integer, DefInt I is misleading. In the following code, the variable individuals defaults to the integer data type but idiots is explicitly declared as a collection. DefInt H-K Dim individuals Dim idiots As New Collection These declarations can cause great confusion later in the code. If you know that all variables starting with the letters H through K are integers, you may misunderstand the meaning of the variable idiots. The following code could be quite confusing. MsgBox idiots(individuals) Deftype statements are anachronisms. Do not use them. Use Hungarian NotationHungarian notation is an extension of the practice of using the first letter in a variable name to indicate its data type. Since Visual Basics variables can have relatively long names, Hungarian notation can add several characters to indicate the variables data type and scope. Hungarian SpecificsHungarian notation consists of several prefix characters identifying a variables scope and data type, followed by the variables name. The prefix characters are usually lowercase and the variable name usually begins with a capital letter as in cmdExit. Table 3.1 lists the prefixes recommended by Microsoft to indicate a variables scope. This prefix should be the first letter in the variables name. Routine local variables have no scope letter in this system. Table 3.2 lists some of the prefixes recommended by Microsoft to indicate a variables data type. These three-letter codes come after the scope letter. It is generally better not to use the udt value to indicate a generic user-defined type. Instead, invent a new three-letter abbreviation for the specific user-defined type. For example, for the user-defined type named Player, you might use the abbreviation plr. Of course when you invent a new abbreviation, you must be certain all developers use the same value. It will be confusing if some programmers use plr and others use ply. You can use three-letter prefixes to indicate the types of controls as well as variables. Table 3.3 gives Microsofts prefix recommendations for the most common control types. For more complete lists of Microsofts recommendations, see the Books Online that comes on the Visual Basic CD-ROM.
Because controls are always defined at a form level, control names usually do not include a scope prefix. For example, an Exit command button might be named cmdExit. On the other hand, a variable reference declared within the code does have a meaningful scope so it can include a scope prefix. For example, suppose a form contains a group of option buttons. When the user clicks one, the following code saves a reference to that button in the forms moptSelection variable.
Private moptSelection As OptionButton
Private Sub optOpenFile_Click()
set moptSelection = optOpenFile
End Sub
Private Sub optCloseFile_Click()
set moptSelection = optCloseFile
End Sub
:
You can also define your own prefixes for classes, related constants, and enumerated types. A group of constants that specify different user types might all have names beginning with usr.
|
|||||||||||||||||||||||||||||||||||||||||||||||||
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|